layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    $.ajax(Feng.ctxPath + '/metadata/list', {
        success: function (data) {
            data = data.data;
            //为数据交付元数据下拉框赋值
            $("#metadataName4").empty();
            $("#metadataName4").append("<option value=''>请选择元数据</option>");
            for (let i = 0; i < data.length; i++) {
                $("#metadataName4").append(new Option(data[i].metadataName, data[i].metadataId))
            }
            //重新渲染
            form.render("select");
        }
    })

    /**
     * 数据交付表管理
     */
    var Delivery = {
        tableId: "deliveryTable"
    };

    /**
     * 初始化表格的列
     */
    Delivery.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'dataDeliveryId', hide: true, title: '数据交付id'},
            {field: 'metadataName', sort: false, title: '元数据', align: "center"},
            {field: 'dataNotDeliveryNumber', sort: false, title: '未交付数据量', align: "center"},
            {field: 'isDelete', sort: false, title: '是否删除；0代表删除；1代表可用', hide: true},
            {align: 'center', toolbar: '#tableBar2', title: '操作', align: "center"}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Delivery.search = function () {
        var queryData = {};

        if ($("#metadataName4 option:selected").text() == "请选择元数据") {
            queryData['metadataName'] = ""
        } else {
            queryData['metadataName'] = $("#metadataName4 option:selected").text()
        }

        table.reload(Delivery.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    //TODO 批量交付

    $("#btnAllDelivery").click(function () {
        Feng.error("功能未实现")
        return;
    })

    //TODO  交付记录

    $("#btnDeliveryRecord").click(function () {
        Feng.error("功能未实现")
        return;
    })

    // 渲染表格
    var tableResult4 = table.render({
        elem: '#' + Delivery.tableId,
        url: Feng.ctxPath + '/delivery/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        skin: "line",
        cols: Delivery.initColumn(),
        done: function (res, curr, count) {
            $('tr').css('background-color', '#ffffff')
            table_data4 = res.data;
        }
    });

    /**
     * 获取表格复选框内的值
     */
    var table_data4 = new Array();
    var ids4 = new Array();
    table.on('checkbox(' + Delivery.tableId + ')', function (obj) {
        console.log(obj)
        if (obj.checked == true) {
            if (obj.type == 'one') {
                ids4.push(obj.data.dataDeliveryId);
            } else {
                for (var i = 0; i < table_data4.length; i++) {
                    ids4.push(table_data4[i].dataDeliveryId);
                }
            }
        } else {
            if (obj.type == 'one') {
                for (var i = 0; i < ids4.length; i++) {
                    if (ids4[i] == obj.data.dataDeliveryId) {
                        ids4.splice(i, 1);
                    }
                }
            } else {
                for (var i = 0; i < ids4.length; i++) {
                    for (var j = 0; j < table_data4.length; j++) {
                        if (ids4[i] == table_data4[j].dataDeliveryId) {
                            ids4.splice(i, 1);
                        }
                    }
                }
            }
        }
    });

    /**
     * 工具栏查看数据
     */
    Delivery.onLookData2 = function (data) {
        //TODO 工具栏查看数据
    }

    /**
     * 工具栏交付
     */
    Delivery.onDelivery = function (data) {
        //TODO 工具栏交付
    }

    // 搜索按钮点击事件
    $('#btnSearch2').click(function () {
        Delivery.search();
    });

    // 工具条点击事件
    table.on('tool(' + Delivery.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'lookData') {
            Delivery.onLookData2(data);
        } else if (layEvent === 'delivery') {
            Delivery.onDelivery(data);
        }
    });
});
